<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :cols="2" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">确 认</multiuse-button>
    </div>
  </div>
</template>
<script>
// import {detailFundAdjust,confirmFundAdjust,getTtFiAccountList,dealerAccount} from '@/api/omSale/OrderManagement/OrderAuditConfirm';
import { dealerAccount, confirmFundAdjust } from '@/api/omSale/OrderManagement/OrderAuditConfirm';
import { lockOrderForEdit } from '../../../../api/part/partPurchase/purchaseOrder';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      aclist: [],
    };
  },
  props: ['list'],
  created() {
    this.gettmFiAccountType(this.list);
    this.getDetail(this.list);
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '所选订单总金额',
          fieldName: 'allAfterDiscountRebateAmount',
          placeholder: '请选择',
          disabled: true,
          precision: 2,
        },
        {
          type: 'INPUT',
          label: '账户总金额',
          fieldName: 'allAvailableAmount',
          placeholder: '请输入',
          disabled: true,
          precision: 2,
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: (p) => {
            if (p) {
              this.changeAccountType(p);
            } else {
              this.formList.find((x) => x.fieldName == 'zhkyye').initialValue = '';
            }
          },
        },
        {
          type: 'INPUT',
          label: '账户可用余额',
          fieldName: 'zhkyye',
          placeholder: '请输入',
          disabled: true,
          precision: 2,
        },
      ];
    },
    async getDetail(list) {
      let a = 0.0;
      list.forEach((e) => {
        a += Number(e.sapSettlementPrice);
      });
      this.formList.find((x) => x.fieldName == 'allAfterDiscountRebateAmount').initialValue = a;
    },
    // 加载资金类型
    async gettmFiAccountType(list) {
      let json = { dealerCode: list[0].dealerCode /*,powerType:list[0].powerCompany,fundsType:0*/ };
      let res = (await dealerAccount(json)).data;
      this.aclist = res;
      // this.formList.find(x=>x.fieldName=='accountId').itemList=res.map(x=>({text:`${x.accountName}--${x.availableAmount}元`,value:x.accountId}))
      this.formList.find((x) => x.fieldName == 'accountId').itemList = res.map((x) => ({ text: x.accountTypeName, value: x.accountId }));
      this.formList.find((x) => x.fieldName == 'allAvailableAmount').initialValue = this.aclist[0].totalFuelAmount;
    },
    changeAccountType(id) {
      let str = '';
      let arr = this.aclist;
      arr.map((x) => {
        if (id == x.accountId) {
          if (x.accountType === 60051002) {
            str = x.ownFund;
          } else {
            str = x.tripFund;
          }
        }
      });
      this.formList.find((x) => x.fieldName == 'zhkyye').initialValue = str;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let arr = [];
          this.list.map((x) => {
            arr.push(x.id);
          });
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.id = json.accountId;
          json.ids = arr;
          let res = await confirmFundAdjust(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '调整成功',
              type: 'success',
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
  },
};
</script>
